<template>
  <basic-container>
    <avue-crud :option="option" :table-loading="loading" :data="data" ref="crud" v-model="form" :permission="permissionList" @row-del="rowDel" @row-update="rowUpdate" @row-save="rowSave" :before-open="beforeOpen" @selection-change="selectionChange" @refresh-change="refreshChange" @on-load="onLoad">
      <template slot="menuRight">
        <table-right :crud='$refs.crud' :data='data' :searchBtn='false' :columnBtn='false' :addPermission="permission.materialtype_add" :handleDelete="handleDelete" :delPermission="permission.materialtype_delete" :refreshChange='refreshChange'>
        </table-right>
      </template>
      <template slot-scope="{row}" slot="status">
        {{row.status==1?'在用':'停产'}}
      </template>
      <template slot-scope="{row}" slot="menu">
        <el-tooltip class="item" effect="dark" content="查看" placement="bottom">
          <el-button icon="el-icon-view" size="small" type="text" @click.stop="$refs.crud.rowView(row,row.$index)" v-if="permission.materialtype_view"></el-button>
        </el-tooltip>
        <el-tooltip class="item" effect="dark" content="编辑" placement="bottom">
          <el-button icon="el-icon-edit" size="small" type="text" @click.stop="$refs.crud.rowEdit(row,row.$index)" v-if="permission.materialtype_edit"></el-button>
        </el-tooltip>
        <el-tooltip class="item" effect="dark" content="删除" placement="bottom">
          <el-button icon="el-icon-delete" size="small" type="text" @click.stop="rowDel(row)" v-if="permission.materialtype_delete"></el-button>
        </el-tooltip>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import {
  getMaterialtypeList,
  addMaterialtype,
  removeMaterialtype,
  updateMaterialtype,
  getMaterialtype,
} from '@/api/project/productData/baseData';
import { mapGetters } from 'vuex';
import { init } from '@/mixins/init';
export default {
  mixins: [init],
  data() {
    return {
      option: {
        size: '',
        page: false,
        // height: 'auto',
        dialogHeight: '100%',
        dialogWidth: '700',
        calcHeight: 80,
        tip: false,
        searchShow: true,
        border: true,
        index: true,
        indexLabel: '序号',
        selection: true,
        viewBtn: false,
        delBtn: false,
        editBtn: false,
        addBtn: false,
        menuWidth: 100,
        dialogClickModal: false,
        labelWidth: '100',
        searchLabelWidth: 100,
        searchMenuSpan: 8,
        column: [
          {
            label: '材料类型',
            prop: 'name',
            search: true,
            searchSpan: 8,
            span: 24,
            maxlength: 30,
            rules: [
              {
                required: true,
                message: '请输入材料类型',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '原材料标识',
            prop: 'rawMaterialFlag',
            span: 24,
            search: true,
            searchSpan: 8,
            // hide: true,
            // showColumn: false,
            type: 'select',
            dicUrl: '/api/blade-system/dict-biz/dictionary?code=isFlag',
            props: {
              label: 'dictValue',
              value: 'dictKey',
            },
            rules: [
              {
                required: true,
                message: '请选择原材料标识',
                trigger: 'blur',
              },
            ],
          },
        ],
      },
      data: [],
    };
  },
  watch: {},
  computed: {
    ...mapGetters(['userInfo', 'permission']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.materialtype_add, false),
        viewBtn: this.vaildData(this.permission.materialtype_view, false),
        delBtn: this.vaildData(this.permission.materialtype_delete, false),
        editBtn: this.vaildData(this.permission.materialtype_edit, false),
      };
    },
  },
  mounted() {},
  created() {
    this.onLoad();
  },
  methods: {
    rowSave(row, done, loading) {
      addMaterialtype(row).then(
        () => {
          this.onLoad();
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
          done();
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      updateMaterialtype(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
          done();
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          return removeMaterialtype(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据');
        return;
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          return removeMaterialtype(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (['edit', 'view'].includes(type)) {
        getMaterialtype(this.form.id).then((res) => {
          this.form = res.data.data;
          this.form.status = res.data.data.status + '';
        });
      } else {
        this.form.status = '1';
      }
      done();
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getMaterialtypeList(null, null, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.data = data;
        this.loading = false;
        this.selectionClear();
      });
    },
  },
};
</script>

<style>
.box {
  height: 800px;
}

.el-scrollbar {
  height: 100%;
}

.box .el-scrollbar__wrap {
  overflow: scroll;
}
</style>
